<?php
/**
 * 考试预约管理
 * @author Administrator
 */
class exam extends admin{
   public function __construct(){
      parent::__construct();
   }

   //考试预约
   public function index(){
      $school_id=(int)$_SESSION['client']['jx_id'];
      $where=' where school_id='.$school_id;
      $map = '';
      if(isset($_GET['k'])&&$_GET['k']!='') {
         $k=trim($_GET['k']);
         if(preg_match('/^1[3-9][0-9]{9}$/',$k)) $map.=' and mobile like "%'.$k.'%"';
         else $map.=' and real_name like "%'.$k.'%"';
      }
      if(isset($_GET['kemu'])&&(int)$_GET['kemu']>0) $map.=' and kemu="'.$_GET['kemu'].'"';
      if(isset($_GET['status'])){
         if(is_numeric($_GET['status'])) $map.=' and status='.(int)$_GET['status'];
         else $map.=' and status>=0';
      }

      if ($map=='') $where.=' and status<2';
      else $where .=$map;

      $total=db()->getOne('select count(*) from x_exam '.$where);
      if(isset($_GET['pagesize'])&&(int)$_GET['pagesize']>0) $pagesize=(int)$_GET['pagesize'];
      else $pagesize=C('PAGE_SIZE');
      if(isset($_GET['p'])&&(int)$_GET['p']>0) $p=(int)$_GET['p'];
      else $p=1;
      $max_p = ceil($total/$pagesize);
      if($p>=$max_p) $p = $max_p;
      $limit=' limit '.($p-1)*$pagesize.','.$pagesize;
      $list=db()->getAll('select * from x_exam '.$where. ' order by id DESC '.$limit);
      $page = array('total' => $total, 'pagesize' => $pagesize, 'curpage' => $p);
      $this->ajaxReturn(array('status'=>1,'data'=>$list,'page'=>$page));
   }

   //编辑用户
   public function update(){
      if(empty($_POST['id'])) $this->ajaxReturn(array('status'=>0,'info'=>'缺少参数'));
      if($_POST['status']<=0) $this->ajaxReturn(array('status'=>0,'info'=>'缺少参数'));
      $id=implode(",",$_POST['id']);
      $status=$_POST['status'];
      if($status==1){//预约成功
         $data['exam_time']=strtotime($_POST['exam_time']);
         $data['exam_address']=trim($_POST['exam_address']);
      }
      $data['status']=$status;
      $data['remark']=remove_xss($_POST['remark']);
      $affert_num=db()->update('x_exam',$data,'id in ('.$id.')');
      $tpl1="您的考试已预约成功，时间：".date('Y-m-d',$_POST['exam_time'])."号".date('H:i',$_POST['exam_time'])."点。地点：".$_POST['exam_address'];
      if ($affert_num>0) {
         //预约成功发短信
         if($status==1){
            $res = db()->getAll('select mobile from x_exam where id in('.$id.')');
            foreach ($res as $v) {
      			send_sms($v['mobile'],$tpl1);
            }
         }
         //考试通过更新扩展信息
			elseif($status==2){
            $res = db()->getAll('select uid,kemu,exam_time from x_exam where id in('.$id.')');
            foreach ($res as $v) {
               $u['kemu'] = $v['kemu']==4?100:($v['kemu']+1);
               $u['km'.$v['kemu'].'_pass_time'] = $v['exam_time'];
               db()->update('x_student_extend',$u,'id='.$v['uid']);
            }
			}
         insertlogs('update','批量修改考试状态',$data,$id);
         $this->ajaxReturn(array('status'=>'1','info'=>'操作成功'));
      }
      elseif($affert_num==0) $this->ajaxReturn(array('status'=>'1','info'=>'操作成功'));
      elseif($affert_num===false) $this->ajaxReturn(array('status'=>'0','info'=>'操作失败'));
    }
}
